using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteECSWeb;

public partial class UControls_CompanyProfileBasicInfo : System.Web.UI.UserControl
{
    LcompanyBLL lCompanyAPI =null;
    int companyIDtable = 0;

    #region Props
    private string _companyUserName = "";

    public string CompanyUserName
    {
        get { return _companyUserName; }
        set { _companyUserName = value; }
    }

    #endregion

    #region OverrideControlMethods
    protected override void OnInit(EventArgs e)
    {
        Page.RegisterRequiresControlState(this);
        base.OnInit(e);
    }
    protected override void LoadControlState(object savedState)
    {
        object[] ctlState = (object[])savedState;
        base.LoadControlState(ctlState[0]);
        _companyUserName = ctlState[1].ToString();
    }
    protected override object SaveControlState()
    {
        object[] ctlState = new object[2];
        ctlState[0] = base.SaveControlState();
        ctlState[1] = _companyUserName;
        return ctlState;
    }
    #endregion
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack || this.Page.IsCallback)
        {
            if (this.CompanyUserName.Length > 0)
            {

                MembershipUser mu = Membership.GetUser(CompanyUserName);

                lCompanyAPI = new LcompanyBLL();
                EcsSiteDS.LcompanyDataTable lCompanyTable =
                    lCompanyAPI.GetLcompanyByCompEmail(mu.Email);
                EcsSiteDS.LcompanyRow lCompanyRow = lCompanyTable[0];

                txt_LcompanyFNameAr.TextBoxData = lCompanyRow.Comp_fulnmar;
                txt_LcompanyFNameEn.TextBoxData = lCompanyRow.Comp_fulnmen;
                txt_LcompanySNameAr.TextBoxData = lCompanyRow.Comp_smlnmar;
                txt_LcompanySNameEn.TextBoxData = lCompanyRow.Comp_smlnmen;
                txt_LcompanyAddressAr.TextBoxData = lCompanyRow.Comp_addrsar;
                txt_LcompanyAddressEn.TextBoxData = lCompanyRow.Comp_addrsen;
                txt_LCompanyComCode.TextBoxData = lCompanyRow.Comp_comircod.ToString();
                txt_LCompanyEstablishYear.TextBoxData = lCompanyRow.EstablishmentYear;

                txt_EmployeeCount.TextBoxData = lCompanyRow.Comp_NumberEmp;
                txt_LcompanyBreifAr.TextBoxData = lCompanyRow.Comp_breifar;
                txt_LcompanyBreifEn.TextBoxData = lCompanyRow.Comp_breifen;
                txt_LcompanyProductAr.TextBoxData = lCompanyRow.Comp_ProductRangeAr;
                txt_LcompanyProductEn.TextBoxData = lCompanyRow.Comp_ProductRangeEn;

                companyIDtable = lCompanyRow.Comp_id;
                cddlCity.UseContextKeyCascade = true;
                cddlCity.SelectedValueCascade = lCompanyRow.City_id.ToString();

                ProductCategoryBLL productCatAPI = new ProductCategoryBLL();
                EcsSiteDS.ProductCategoryDataTable productCatTable =
                    productCatAPI.GetProductCategoryByProductID(lCompanyRow.Comp_Sector_id);
                EcsSiteDS.ProductCategoryRow productCatRow = productCatTable[0];
                if (productCatRow.IsProdCat_GropIDNull())
                {
                    cddlProductGrand.UseContextKeyCascade = true;
                    cddlProductGrand.SelectedValueCascade = productCatRow.ProdCat_Id.ToString();
                }
                else
                {
                    cddlProductGrand.UseContextKeyCascade = true;
                    cddlProductGrand.SelectedValueCascade = productCatRow.ProdCat_GropID.ToString();
                    cddlProductFsther.ContextKeyCascade = productCatRow.ProdCat_Id.ToString();
                }
            }
        }
    }

    public int SaveCompanyProfile()
    {

        lCompanyAPI= new LcompanyBLL();
       
        int ProductID=0;
        int CityID=0;
        int CompanyID= 0;
       
        if (string.IsNullOrEmpty(this.cddlProductFsther.dllListItemsValue))
        {
            if (!string.IsNullOrEmpty(this.cddlProductGrand.dllListItemsValue))
            {
                ProductID = int.Parse(this.cddlProductGrand.dllListItemsValue);
            }
        }
        else
        {
            ProductID = int.Parse(this.cddlProductFsther.dllListItemsValue);
        }
        if (!string.IsNullOrEmpty(this.cddlCity.dllListItemsValue))
        {
            CityID = int.Parse(this.cddlCity.dllListItemsValue);
        }

        if (this.CompanyUserName.Length > 0)
        {
            lCompanyAPI.UpdateLcompany(this.txt_LcompanyFNameAr.TextBoxData,
                this.txt_LcompanySNameAr.TextBoxData, this.txt_LcompanyAddressAr.TextBoxData,
                this.txt_LcompanyBreifAr.TextBoxData, this.txt_LcompanyFNameEn.TextBoxData,
                this.txt_LcompanySNameEn.TextBoxData, this.txt_LcompanyAddressEn.TextBoxData,
                this.txt_LcompanyBreifEn.TextBoxData, int.Parse(this.txt_LCompanyComCode.TextBoxData),
                this.txt_EmployeeCount.TextBoxData, this.txt_LCompanyEstablishYear.TextBoxData,
                CityID, ProductID, this.txt_LcompanyProductAr.TextBoxData,
                this.txt_LcompanyProductEn.TextBoxData, companyIDtable);
            return companyIDtable;
        }
        else
        {
            CompanyID = lCompanyAPI.AddLcompany(this.txt_LcompanyFNameAr.TextBoxData,
                this.txt_LcompanyFNameEn.TextBoxData,
                this.txt_LcompanySNameAr.TextBoxData, this.txt_LcompanySNameEn.TextBoxData,
                this.txt_LcompanyAddressAr.TextBoxData,
                this.txt_LcompanyAddressEn.TextBoxData, int.Parse(this.txt_LCompanyComCode.TextBoxData),
                this.txt_LCompanyEstablishYear.TextBoxData,
                CityID, this.txt_EmployeeCount.TextBoxData, this.txt_LcompanyBreifAr.TextBoxData,
                this.txt_LcompanyBreifEn.TextBoxData, ProductID, this.txt_LcompanyProductAr.TextBoxData,
                this.txt_LcompanyProductEn.TextBoxData);
            return CompanyID;
        }
    }
}
